package com.ysqcould.common.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;
import java.net.URL;
import java.util.List;


public class OssClientUtil {

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private static final String ENDPOINT = "http://oss-cn-shenzhen.aliyuncs.com";
    //阿里云主账号AccessKey拥有所有API的访问权限
    private static final String ACCESS_KEY_ID = "LTAI4GFHDtc6xbFWsQf7hWQc";
    //access_key_secret
    private static final String ACCESS_KEY_SECRET = "N0OOTD9NS8cGeMr1dFQUdPFUbk3IMa";
    //bucket_name
    private static final String BUCKET_NAME = "shequncloud";

    /**
     * 上传文件/图片到阿里oss
     * @param fileName 文件名称
     * @param inputStream 文件输入流
     * @return uploadUrl
     */
    public static String uploadPictures(String fileName, InputStream inputStream){
        //文件目录
        String fileDir = "ht-images/test/";
        String saveUrl = fileDir+fileName;
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // 上传网络流。
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, saveUrl, inputStream);
        final PutObjectResult result = ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        System.out.println(saveUrl);
        //图片地址
        String uploadUrl = "https://"+BUCKET_NAME+"."+ENDPOINT.split("//")[1]+"/"+saveUrl;
        return uploadUrl;
    }

    /**
     * 删除oss上的图片
     * @param name 图片名称
     * @return String
     */
    public static String deletePicturesByName(String name){
        //文件目录
        String fileDir = "ht-images/test/";
        String delUrl = fileDir+name;
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        //删除图片
        ossClient.deleteObject(BUCKET_NAME,delUrl);
        ossClient.shutdown();
        return "SUCCESS";
    }

    /**
     * 批量删除图片
     */
    public static String deleteListByName(List<String> nList){
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        //删除图片
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(BUCKET_NAME).withKeys(nList));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        ossClient.shutdown();
        return "SUCCESS";
    }

    /**
     * 上传网络图片url
     * @param imageUrl          图片地址
     */
    public static String uploadUrlImage(String imageUrl){
        //文件目录
        String fileDir = "ht-images/netImage/";

        String suffix = "." + imageUrl.substring(imageUrl.lastIndexOf('=') + 1);
        try {
            String saveUrl = fileDir+ MD5Utils.getMD5(imageUrl);
            InputStream inputStream = new URL(imageUrl).openStream();
            //创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            // 上传网络流。
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, saveUrl+suffix, inputStream);
            ossClient.putObject(putObjectRequest);
            ossClient.shutdown();
            //图片地址
            return "https://"+BUCKET_NAME+"."+ENDPOINT.split("//")[1]+"/"+saveUrl+suffix;
        }catch (Exception e){
            System.out.println(String.format("图片流异常：%s",e.getMessage()));
            e.printStackTrace();
        }
        return null;
    }

}
